package cn.edu.tsinghua.mutimedia.format;

import java.io.IOException;  
import org.apache.hadoop.io.Text;  
import org.apache.hadoop.mapreduce.InputSplit;
import org.apache.hadoop.mapreduce.RecordReader;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
  
  
public class ImageFileRecordReader extends RecordReader<Text, Text> {  
    
	  	private FileSplit fileSplit;
	    private TaskAttemptContext context;
	    private boolean flag;

	    public ImageFileRecordReader(FileSplit fileSplit, TaskAttemptContext context, boolean flag) {
	        this.fileSplit = fileSplit;
	        this.context = context;
	        this.flag = flag;
	    }

	    @Override
	    public void initialize(InputSplit split, TaskAttemptContext context) throws IOException, InterruptedException {
	        split = fileSplit;
	        context = this.context;
	    }

	    @Override
	    public boolean nextKeyValue() throws IOException, InterruptedException {
	        if(flag){
	            flag = false;
	            return true;
	        }
	        return false;
	    }

	    @Override
	    public Text getCurrentKey() throws IOException, InterruptedException {
	        return new Text(fileSplit.getPath().toString());
	    }

	    @Override
	    public Text getCurrentValue() throws IOException, InterruptedException {
	        try {
	            if(fileSplit.getLocations().length > 0){
	                return new Text(fileSplit.getLocations()[0]);
	            }else {
	                return new Text("nopathget");
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        return null;
	    }

	    @Override
	    public void close() throws IOException {
	        //To change body of implemented methods use File | Settings | File Templates.
	    }

	    @Override
	    public float getProgress() throws IOException {
	        return 0;  //To change body of implemented methods use File | Settings | File Templates.
	    }
}